package com.wlb365.financing.common.shiro;

import cn.hutool.core.util.StrUtil;
import com.wlb365.financing.common.utils.CryptionKit;
import com.wlb365.financing.common.constants.SysConstant;
import com.wlb365.financing.common.utils.StringUtils;
import com.wlb365.financing.modules.sys.entity.Menu;
import com.wlb365.financing.modules.sys.entity.User;
import com.wlb365.financing.modules.sys.service.IMenuService;
import com.wlb365.financing.modules.sys.service.IUserRoleService;
import com.wlb365.financing.modules.sys.service.IUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * <p>
 * 概述：认证授权
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2020/3/21
 */
@Component
public class UserRealm extends AuthorizingRealm {

    @Autowired
    @Lazy
    private IUserService userService;

    @Autowired
    @Lazy
    private IMenuService menuService;

    @Autowired
    @Lazy
    private IUserRoleService userRoleService;

    /**
     * 授权(验证权限时调用)
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        User user = (User) principals.getPrimaryPrincipal();
        String userId = user.getId();
        String username = user.getUsername();

        List<String> permsList;

        // todo 系统管理员，拥有最高权限（不用在系统设置任何权限）
        if (SysConstant.ADMIN.equals(username)) {
            List<Menu> menuList = menuService.list();
            permsList = new ArrayList<>(menuList.size());
            for (Menu menu : menuList) {
                permsList.add(menu.getPermission());
            }
        } else {
            permsList = menuService.getPermission(userId);
        }

        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            if (StrUtil.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);

        String roleNames = userRoleService.getRoleNamesByUserId(userId);
        Set<String> roleSet = new HashSet<>();
        if (StringUtils.isNotEmpty(roleNames)){
            roleSet.addAll(Arrays.asList(roleNames.trim().split(",")));
        }

        info.setRoles(roleSet);

        return info;
    }

    /**
     * 认证(登录时调用)
     *
     * @param authcToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

        //查询用户信息
        User user = userService.getByUsername(token.getUsername());
        //账号不存在
        if (user == null) {
            throw new UnknownAccountException("账号或密码不正确");
        }

        String password = CryptionKit.genUserPwd(new String((char[]) token.getCredentials()));
        if (!user.getPassword().equals(password)) {
            // 密码错误
            throw new IncorrectCredentialsException("账号或密码不正确");
        }

        if (user.getStatus() != SysConstant.VALID_USER) {
            // 账号锁定
            throw new LockedAccountException("账号已被锁定，请联系管理员");
        }

        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }

    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        CustomCredentialsMatcher customCredentialsMatcher = new
                CustomCredentialsMatcher();
        super.setCredentialsMatcher(customCredentialsMatcher);
    }

}
